package com.FrameWork.Utils;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateTimeUtils
{
	public static String GetCurrentTime()
	{
		Calendar calneder = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		return format.format(calneder.getTime());

	}
	/***
	 * 时间格式转换
	 */
	public static String convertTime(String time)
	{
		// 如果时间后缀为Z的要转换成GMT
		if (time.contains("Z"))
		{
			time = time.replace("Z", "GMT");
		}
		Long date = Date.parse(time);
		Date date2 = new Date(date);
		SimpleDateFormat simpleDateFormat = null;
		simpleDateFormat = new SimpleDateFormat("yyyy年 MM月dd日 hh:mm");
		return simpleDateFormat.format(date2);
	}

	public static int getAllSecond(String time)
	{
		int h = Integer.valueOf(time.substring(0, 2));// 时
		int m = Integer.valueOf(time.substring(2, 4));// 分
		int s = Integer.valueOf(time.substring(4, 6));// 秒

		int allS = h * 60 * 60 + m * 60 + s;
		return allS;
	}

	// dateStr = 2011-12-02
	// return 20111202
	public static String getChar14Date(String dateStr)
	{
		if (null != dateStr && !"".equals(dateStr))
		{
			dateStr = dateStr.replaceAll("-", "");
			return dateStr;
		}
		else
		{
			return "";
		}

	}

	public static String getEditTextDate(String getDate)
	{
		
		return getDate.substring(0, 4)
		+ "-"
		+ getDate.substring(4, 6)
		+ "-"
		+ getDate.substring(6, 8);

	}

	public static String getEditTextDate(int year,
			int monthOfYear,
			int dayOfMonth)
	{
		return year
		+ "-"
		+ (monthOfYear < 10
				? "0" + String.valueOf((monthOfYear))
						: (monthOfYear))
						+ "-"
						+ (dayOfMonth < 10
								? "0" + String.valueOf(dayOfMonth)
										: dayOfMonth);
	}

	/**
	 * 计算时间耗时。
	 * 
	 * @param dateTime_Start2
	 * @param dateTime_End
	 * @return
	 */
	public static String getElapsedTime(String dateTime_Start2,
			String dateTime_End)
	{
		// 获得秒级别的耗时。
		int s_Start = getAllSecond(dateTime_Start2.substring(8));
		int s_End = getAllSecond(dateTime_End.substring(8));
		int Elapsed = s_End - s_Start;

		// 分离时分秒
		int h = Elapsed / 3600;
		Elapsed = Elapsed - h * 3600;
		int m = Elapsed / 60;
		Elapsed = Elapsed - m * 60;
		int s = Elapsed;

		// 格式化时分秒
		String sH = String.valueOf(h);
		if (h < 10)
			sH = "0" + sH;
		String sM = String.valueOf(m);
		if (m < 10)
			sM = "0" + sM;
		String sS = String.valueOf(s);
		if (s < 10)
			sS = "0" + sS;

		// 拼接时分秒。
		return String.format("%s:%s:%s", sH, sM, sS);
	}

	/**
	 * 返回14位时间字符串中的，时、分，格式为：XX:XX
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getHM(String strDate)
	{
		String temp = "";
		if (strDate.length() >= 14)
		{
			temp += strDate.substring(8, 10);
			temp += ":";
			temp += strDate.substring(10, 12);
		}
		return temp;
	}

	/**
	 * 返回14位时间字符串中的，时、分、秒，格式为：XX:XX:XX
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getHMS(String strDate)
	{
		String temp = "";
		if (strDate.length() >= 14)
		{
			temp += strDate.substring(8, 10);
			temp += ":";
			temp += strDate.substring(10, 12);
			temp += ":";
			temp += strDate.substring(12, 14);
		}
		return temp;
	}

	public static String getSelects12Date(String strDate)
	{
		String temp = "";
		if (strDate.length() >= 14)
		{
			temp = strDate.substring(0, 4);
			temp += "年";
			temp += strDate.substring(4, 6);
			temp += "月";
			temp += strDate.substring(6, 8);
			temp += "日";
			temp += strDate.substring(8, 10);
			temp += ":";
			temp += strDate.substring(10, 12);
		}
		return temp;
	}

	

	public static String getSelectsDate(String strDate)
	{
		String temp = "";
		if (strDate.length() >= 14)
		{
			temp = strDate.substring(0, 4);
			temp += "年";
			temp += strDate.substring(4, 6);
			temp += "月";
			temp += strDate.substring(6, 8);
			temp += "日";
			temp += strDate.substring(8, 10);
			temp += ":";
			temp += strDate.substring(10, 12);
			temp += ":";
			temp += strDate.substring(12, 14);
		}
		return temp;
	}

	public static String getSelectsDateString(String strDate)
	{
		StringBuilder temp = new StringBuilder();
		if (strDate.length() >= 14)
		{
			temp.append(strDate.substring(0, 4));
			temp.append("年");
			temp.append(strDate.substring(4, 6));
			temp.append("月");
			temp.append(strDate.substring(6, 8));
			temp.append("日");
		}
		return temp.toString();
	}
	/**
	 * 通过毫秒变成当前需要的格式的日期
	 * @param milliseconds
	 * @return
	 */
	public static String getLongDateFromMillis(long milliseconds){
		SimpleDateFormat formatter=new SimpleDateFormat("yyyyMMddHHmmss",Locale.CHINA);
		Date date=new Date(milliseconds);
		return formatter.format(date);
	}
	
	public static long getMillisFromFromLongDate(String time){
		SimpleDateFormat formatter=new SimpleDateFormat("yyyyMMddHHmmss",Locale.CHINA);
		try {
			return formatter.parse(time).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	
}
